package com.dl7.mvp;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.danikula.videocache.HttpProxyCacheServer;
import com.danikula.videocache.file.FileNameGenerator;
import com.dl7.downloaderlib.DownloadConfig;
import com.dl7.downloaderlib.FileDownloader;
import com.dl7.mvp.api.RetrofitService;
import com.dl7.mvp.engine.DownloaderWrapper;
import com.dl7.mvp.injector.components.ApplicationComponent;
import com.dl7.mvp.injector.components.DaggerApplicationComponent;
import com.dl7.mvp.injector.modules.ApplicationModule;
import com.dl7.mvp.local.table.AlbumsListBean;
import com.dl7.mvp.local.table.DaoMaster;
import com.dl7.mvp.local.table.DaoSession;
import com.dl7.mvp.rxbus.RxBus;
import com.dl7.mvp.utils.PreferencesUtils;
import com.dl7.mvp.utils.StringUtils;
import com.dl7.mvp.utils.ToastUtils;

import org.greenrobot.greendao.database.Database;

import java.io.File;
import java.util.List;

public class XApp extends Application {

    private static final String DB_NAME = "news-db";

    private static ApplicationComponent sAppComponent;
    private static Context sContext;
    private DaoSession mDaoSession;
    // 因为下载那边需要用，这里在外面实例化在通过 ApplicationModule 设置
    private RxBus mRxBus = new RxBus();
    // 边播边存
    private HttpProxyCacheServer proxy;

    private static List<AlbumsListBean.DataBean> mCategoryList;
    public static String mPlayingUrl = "";

    @Override
    public void onCreate() {
        super.onCreate();
        sContext = getApplicationContext();
        _initDatabase();
        _initInjector();
        _initConfig();
    }

    /**
     * 使用Tinker生成Application，这里改成静态调用
     *
     * @return
     */
    public static ApplicationComponent getAppComponent() {
        return sAppComponent;
    }

    public static Context getContext() {
        return sContext;
    }

    public static List<AlbumsListBean.DataBean> getCategoryList() {
        Log.e("12345", "getCategoryList: " +mCategoryList );
        return mCategoryList;
    }

    /**
     * 初始化注射器
     */
    private void _initInjector() {
        // 这里不做注入操作，只提供一些全局单例数据
        sAppComponent = DaggerApplicationComponent.builder()
                .applicationModule(new ApplicationModule(this, mDaoSession, mRxBus))
                .build();
    }

    /**
     * 初始化数据库
     */
    private void _initDatabase() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(sContext, DB_NAME);
        Database database = helper.getWritableDb();
        mDaoSession = new DaoMaster(database).newSession();
    }

    /**
     * 初始化配置
     */
    private void _initConfig() {
        RetrofitService.init();
        ToastUtils.init(sContext);
        DownloaderWrapper.init(mRxBus, mDaoSession.getVideoInfoDao());
        FileDownloader.init(sContext);
        DownloadConfig config = new DownloadConfig.Builder()
                .setDownloadDir(PreferencesUtils.getSavePath(sContext) + File.separator + "video/").build();
        FileDownloader.setConfig(config);
    }

    /**
     * 初始化VideoCache
     */


    public static HttpProxyCacheServer getProxy(Context context) {
        XApp app = (XApp) context.getApplicationContext();
        return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
    }

    private HttpProxyCacheServer newProxy() {
        return new HttpProxyCacheServer.Builder(this)
                .cacheDirectory(new File(PreferencesUtils.getSavePath(sContext) + File.separator + "video/"))
                .maxCacheSize(1024 * 1024 * 1024)
                .fileNameGenerator(new MyFileNameGenerator())
                .build();
    }

    public class MyFileNameGenerator implements FileNameGenerator {
        public String generate(String url) {
            return StringUtils.clipFileName(url);
        }
    }
}
